using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Runtime.Remoting.Messaging;
using System.Windows.Forms;
using System.Diagnostics;

namespace FreeFlow.WinFormsControls.HelperClasses
{
	/// <summary>
	/// Summary description for ExceptionForm.
	/// </summary>
	internal class ExceptionForm : System.Windows.Forms.Form
	{
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label errorLabel;
    private System.Windows.Forms.Button yesButton;
    private System.Windows.Forms.Button noButton;
    private System.Windows.Forms.TextBox detailsTextBox;
    private Label label1;
    private TextBox emailTextBox;
    private LinkLabel linkLabel1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public ExceptionForm(Exception exception)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

      errorLabel.Text = "An unexpected error occurred - " + exception.Message + 
        ". Do you want to submit an error report?";

      LogExceptionDelegate logDelegate = new LogExceptionDelegate(LogException); 
      logDelegate.BeginInvoke(exception, new AsyncCallback(LogCallBack), null);
    }

    delegate string LogExceptionDelegate(Exception e);

    private string LogException(Exception exception) 
    {
      return ExceptionLogger.GenerateExceptionLog(exception);
    }

    delegate void ShowExceptionDetailsDelegate(string errorDetails);
    private void ShowExceptionDetails(string errorDetails)
    {
      if (InvokeRequired)
      {
        Invoke(new ShowExceptionDetailsDelegate(ShowExceptionDetails), new object[] { errorDetails });
      }
      else
      {
        detailsTextBox.Text = errorDetails;
      }
    }

    private void LogCallBack(IAsyncResult result)
    {
      LogExceptionDelegate logDelegate = (LogExceptionDelegate)((AsyncResult)result).AsyncDelegate;
      string data = logDelegate.EndInvoke(result);
      ShowExceptionDetails(data);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ExceptionForm));
      this.yesButton = new System.Windows.Forms.Button();
      this.errorLabel = new System.Windows.Forms.Label();
      this.noButton = new System.Windows.Forms.Button();
      this.label2 = new System.Windows.Forms.Label();
      this.detailsTextBox = new System.Windows.Forms.TextBox();
      this.label1 = new System.Windows.Forms.Label();
      this.emailTextBox = new System.Windows.Forms.TextBox();
      this.linkLabel1 = new System.Windows.Forms.LinkLabel();
      this.SuspendLayout();
      // 
      // yesButton
      // 
      this.yesButton.DialogResult = System.Windows.Forms.DialogResult.Yes;
      this.yesButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
      this.yesButton.Location = new System.Drawing.Point(8, 56);
      this.yesButton.Name = "yesButton";
      this.yesButton.Size = new System.Drawing.Size(75, 23);
      this.yesButton.TabIndex = 0;
      this.yesButton.Text = "Yes";
      // 
      // errorLabel
      // 
      this.errorLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.errorLabel.Location = new System.Drawing.Point(16, 8);
      this.errorLabel.Name = "errorLabel";
      this.errorLabel.Size = new System.Drawing.Size(420, 40);
      this.errorLabel.TabIndex = 1;
      this.errorLabel.Text = "errorLabel";
      // 
      // noButton
      // 
      this.noButton.DialogResult = System.Windows.Forms.DialogResult.No;
      this.noButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
      this.noButton.Location = new System.Drawing.Point(89, 56);
      this.noButton.Name = "noButton";
      this.noButton.Size = new System.Drawing.Size(75, 23);
      this.noButton.TabIndex = 2;
      this.noButton.Text = "No";
      // 
      // label2
      // 
      this.label2.Location = new System.Drawing.Point(5, 153);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(240, 16);
      this.label2.TabIndex = 3;
      this.label2.Text = "This is the data that will be submitted";
      // 
      // detailsTextBox
      // 
      this.detailsTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                  | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.detailsTextBox.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
      this.detailsTextBox.Location = new System.Drawing.Point(8, 172);
      this.detailsTextBox.Multiline = true;
      this.detailsTextBox.Name = "detailsTextBox";
      this.detailsTextBox.ReadOnly = true;
      this.detailsTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
      this.detailsTextBox.Size = new System.Drawing.Size(428, 255);
      this.detailsTextBox.TabIndex = 4;
      this.detailsTextBox.Text = "Generating...";
      // 
      // label1
      // 
      this.label1.AutoSize = true;
      this.label1.Location = new System.Drawing.Point(8, 86);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(396, 13);
      this.label1.TabIndex = 5;
      this.label1.Text = "If you would like to be contacted about this bug, please provide your email addre" +
          "ss";
      // 
      // emailTextBox
      // 
      this.emailTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this.emailTextBox.Location = new System.Drawing.Point(11, 103);
      this.emailTextBox.Name = "emailTextBox";
      this.emailTextBox.Size = new System.Drawing.Size(425, 20);
      this.emailTextBox.TabIndex = 6;
      // 
      // linkLabel1
      // 
      this.linkLabel1.AutoSize = true;
      this.linkLabel1.Location = new System.Drawing.Point(8, 130);
      this.linkLabel1.Name = "linkLabel1";
      this.linkLabel1.Size = new System.Drawing.Size(411, 13);
      this.linkLabel1.TabIndex = 7;
      this.linkLabel1.TabStop = true;
      this.linkLabel1.Text = "Click here to view information about known issues, that may help resolve your pro" +
          "blem";
      this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
      // 
      // ExceptionForm
      // 
      this.AcceptButton = this.yesButton;
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.CancelButton = this.noButton;
      this.ClientSize = new System.Drawing.Size(444, 439);
      this.Controls.Add(this.linkLabel1);
      this.Controls.Add(this.emailTextBox);
      this.Controls.Add(this.label1);
      this.Controls.Add(this.detailsTextBox);
      this.Controls.Add(this.label2);
      this.Controls.Add(this.noButton);
      this.Controls.Add(this.errorLabel);
      this.Controls.Add(this.yesButton);
      this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
      this.MinimumSize = new System.Drawing.Size(460, 380);
      this.Name = "ExceptionForm";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Text = "Unexpected error";
      this.ResumeLayout(false);
      this.PerformLayout();

    }
		#endregion

    public string EmailAddress
    {
      get
      {
        return emailTextBox.Text;
      }
    }

    private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
      Process.Start("http://www.freeflownet.com/bugs.aspx");
    }
	}
}
